/*
*   Coder : Dzlua
*   Email : 505544956@qq.com
*   Time  : 2017/08/08
*/
#pragma once

#include "datapkg.hpp"

#include <random>
#include <chrono>

namespace {
    std::default_random_engine g_random_engine(time(nullptr));

    uint16_t dg_random(uint16_t min, uint16_t max) {
        std::uniform_int_distribution<uint16_t> u(min, max);
        return u(g_random_engine);
    }

    // for library
    uint16_t dg_get_library_count() { return 7; }

    std::string dg_get_library(uint16_t idx) {
        switch (idx) {
            case 0: return "Chrome";
            case 1: return "evpp";
            case 2: return "muduo";
            case 3: return "ATLMFC";
            case 4: return "ios源码";
            case 5: return "Linux源码";
            case 6: return "安卓源码";
        }
        return std::move(std::string());
    }

    uint16_t dg_random_library_idx() {
        return dg_random(0, dg_get_library_count() - 1);
    }

    std::string dg_random_library() {
        return std::move( dg_get_library( dg_random_library_idx() ) );
    }
    //-----------------//
    std::string dg_random_so() {
        std::string text;

        auto len = dg_random(1, 100);
        for (uint16_t i = 0; i < len; ++i) {
            auto ch = dg_random(32, 126);
            text.push_back((char)ch);
        }
        return std::move(text);
    }
}

class DataGenerate {
public:
    static std::shared_ptr<DataPkg> Random(const std::string &library = std::string()) {
        auto datapkg = std::make_shared<DataPkg>(1024);
            datapkg->add("so");
            datapkg->add(13);
            datapkg->add(10);

            // add search ctx
            datapkg->add(dg_random_so());

            datapkg->add(13);
            datapkg->add(10);

            // add library
            if (library.empty()) {
                datapkg->add(dg_random_library());
            } else {
                datapkg->add(library);
            }
        return datapkg;
    }
};